Code for take your picture from gallery in Android.
145922-Aug-2018
How to take your picture from the gallery and add in your android project.
Updated on 02-Jul-2023
Home / DeveloperSection / Forums / Code for take your picture from gallery in Android.
Aryan Kumar
02-Jul-2023Sure, here is the code to take your picture from gallery in Android:
public void pickImageFromGallery() { // Check if the storage permission has been granted if (checkStoragePermission()) { // The permission has already been granted Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, 100); } else { // Request the permission ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100); } }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data);
Code snippet
}
This code will first check if the
WRITE_EXTERNAL_STORAGE
permission has already been granted. If it has not been granted, the code will request the permission from the user. If the user grants the permission, the code will start an activity to allow the user to select an image from the gallery.The
startActivityForResult()
method has two parameters:The activity that is started will return a result code. If the user selects an image from the gallery, the result code will be
RESULT_OK
. The image that the user selected will be returned in thedata
parameter of theonActivityResult()
method.In the
onActivityResult()
method, you can set the image in the image view by calling thesetImageURI()
method of the image view.To use this code, you would need to call the
pickImageFromGallery()
method in your app. For example, you could call the method in your app'sonCreate()
method.Here is an example of how you could call the
pickImageFromGallery()
method in your app'sonCreate()
method:Code snippet
When you run the app, you will see an image view. You can click on the image view to pick an image from the gallery.
Arti Mishra
22-Aug-2018/* Create object of ImageView */
ImageView editImage = (ImageView) findViewById(R.id.edit_image);
/* Take your picture from gallery */
editImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent gallery = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
}
});